﻿@page "/gatewayruntime/variable"
@using System.Linq.Expressions;
@using BlazorComponent;
@using Masa.Blazor.Presets;
@using Microsoft.AspNetCore.Authorization;
@using Microsoft.Extensions.DependencyInjection
@using System.Collections.Concurrent
@using ThingsGateway.Admin.Application;
@using ThingsGateway.Admin.Blazor
@using ThingsGateway.Core.Extension
@using ThingsGateway.Foundation
@using ThingsGateway.Gateway.Application

@namespace ThingsGateway.Gateway.Blazor
@attribute [Authorize]
@inject UserResoures UserResoures
@attribute [RolePermission]
@inherits BaseComponentBase

@code {
    private List<Device> _queryDevices = new();
    private List<Device> _queryBusinessDevices = new();
    private void DeviceQuerySelections(string v)
    {
        if (string.IsNullOrEmpty(v))
        {
            return;
        }
        _queryDevices = _devices.Where(e => e.Name.ToLowerInvariant().IndexOf(v.ToLowerInvariant()) > -1).OrderBy(a => a.Name.Length).Take(20).ToList();
    }
    private void BusinessDeviceQuerySelections(string v)
    {
        if (string.IsNullOrEmpty(v))
        {
            return;
        }
        _queryBusinessDevices = _businessDevices.Where(e => e.Name.ToLowerInvariant().IndexOf(v.ToLowerInvariant()) > -1).OrderBy(a => a.Name.Length).Take(20).ToList();
    }


}
<AppDataTable @ref="_datatable" TItem="VariableRunTime" SearchItem="VariablePageInput" AddItem="VariableAddInput" EditItem="VariableEditInput"
              SearchModel="_search" FilterHeaders=FilterHeaders Filters=Filters
                IsShowSearchKey
               QueryCallAsync="QueryCallAsync"
               IsMenuOperTemplate=false AddWidth="1600" EditWidth="1600"
                IsShowDetailButton IsShowOperCol=true
                IsShowQueryButton>

    <SearchTemplate>

        <MTextField Dense Style="max-width:200px;" HideDetails=@("auto") Class="my-1 mx-2 " Outlined Clearable
                     @bind-Value="context.Name" Label=@AppService.I18n.T(context.Description(x => x.Name)) />
         <MTextField Dense Style="max-width:200px;" HideDetails=@("auto") Class="my-1 mx-2 " Outlined Clearable
                     @bind-Value="context.RegisterAddress" Label=@AppService.I18n.T(context.Description(x => x.RegisterAddress)) />

         <MAutocomplete Dense Style="max-width:200px;" HideDetails=@("auto") Class="my-1 mx-2 " CacheItems HideNoData Outlined
                        ItemValue="r=>r.Id" ItemText="r=>r.Name" OnSearchInputUpdate="DeviceQuerySelections"
                        Items="_queryDevices" Flat Clearable
                        @bind-Value="context.DeviceId" Label=@AppService.I18n.T(context.Description(x => x.DeviceId)) />

         <MAutocomplete Dense Style="max-width:200px;" HideDetails=@("auto") Class="my-1 mx-2 " CacheItems HideNoData Outlined
                        ItemValue="r=>r.Id" ItemText="r=>r.Name" OnSearchInputUpdate="BusinessDeviceQuerySelections"
                        Items="_queryBusinessDevices" Flat Clearable
                        @bind-Value="context.BusinessDeviceId" Label=@AppService.I18n.T(context.Description(x => x.BusinessDeviceId)) />

     </SearchTemplate>

     <ItemColOperTemplate>
         <MButton Disabled=@(!UserResoures.IsHasButtonWithRole("GatewayVariableRpc")) Color="info" Text Small
                  OnClick=@(()=>WriteAsync(context.Item))>
             @AppService.I18n.T("远程写入")
        </MButton>

    </ItemColOperTemplate>
    <ItemColTemplate>
        @switch (context.Header.Value)
        {
            case nameof(context.Item.IsOnline):
                <EnableChip Value="context.Item.IsOnline" DisabledLabel=@AppService.I18n.T("离线") EnabledLabel=@AppService.I18n.T("在线")>
                </EnableChip>
                break;
            case nameof(context.Item.Enable):
                <EnableChip Value="context.Item.Enable">
                </EnableChip>
                break;
            case nameof(context.Item.DeviceId):
                @(_devices.FirstOrDefault(a => a.Id == context.Item.DeviceId)?.Name)
                break;

            default:
                @if (context.Header.CellClass?.Contains("text-truncate") == true)
                {
                    <span title=@context.Value>
                        @context.Value
                    </span>
                }
                else
                {
                    @context.Value
                }
                break;
        }

    </ItemColTemplate>
</AppDataTable>




